package cn.dhn_app.horseman_app;

import sun.misc.BASE64Decoder;

import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @Author lyy
 * @Description TODO
 * @PACKAGE_NAME: cn.dhn_app.horseman_app
 * @ClassName Imagebase64
 * @Date 2020/9/12 17:35
 * @DAY_NAME_FULL: 星期六
 * @Version 1.0
 **/
public class Imagebase64 {
    /**
     * base64字符串转换成图片
     * @param imgStr
     * @param path
     * @return
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    public static void main(String[] args) {

//        String base64 = "";

//        generateImage(base64, "E:\\images\\二维码.jpg");
    }
}
